#!/usr/bin/python

import re
import os
import sqlite3

class FileDataBase:
    def __init__(self):
        self.cx = sqlite3.connect("/Users/darkh/.programer_type_DB")
        self.cu = self.cx.cursor()
        try:
            self.cu.execute("""create table type (id integer primary key , type_language text UNIQUE ) """)
            print "hello qingluan"
        except sqlite3.OperationalError :
            pass
    def insert_type(self,pro_type):
        count = self.size()
        self.cu.execute('insert into type values(%d,"%s") '%(count +1,pro_type))
        self.cx.commit()
    def get_connect(self):
        if self.cu : 
            return [self.cx,self.cu]

    def size(self):
        self.cu.execute("select * from type")
        type_all = self.cu.fetchall()
        return len(type_all)

    def get_all_type(self):
        self.cu.execute("select * from type ")
        res = self.cu.fetchall()
        self.cu.close()
        self.cx.close()
        return map((lambda x: x[1]),res)

    def close(self):
        if (self.cu ):
            self.cu.close()
            self.cx.close()
#re to get some
def get_file_type(string):
    compiler = re.compile('(\.[a-z]{1,7})')
    tem = re.findall(compiler,string)
    return tem

def get_file_type_without_point(string):
    compiler = re.compile('(\.[a-z]{1,7})')
    tem = re.findall(compiler,string)
    return tem[-1]

class file_code:
    def __init__(self):
        self.Desk_path = os.getcwd() +"/Desktop/"
        self.DataBase = FileDataBase()
        self.language = self.DataBase.get_all_type()
        self.List_code = []
        self.code_exist_p = self.Judge_code(self.Desk_path)
        self.loop_look()
    def loop_look(self):  #initialized
	if (os.getcwd() != "/Users/darkh/Desktop"):
            if  self.code_exist_p :
                print "Path :%s" % self.Desk_path
                self.format()
            else:
                print "No code file! error1 \n"
                exit(0)
        else:
            self.format()
    
    def Judge_code(self,path): #initialized
        boolean =  False
        file_list =  os.popen("ls " + path).readlines()
        for item in self.language :
            for item2 in file_list:
               try:
                   if item in item2: 
                       print "%s" %item2
                       self.List_code.append(item2)
                       boolean = True
               except UnicodeDecodeError:
                   pass 
        if self.List_code :
            self.List_code = map ((lambda x: x[0:len(x)-1]),self.List_code)
        return boolean

    def update(self):
        if (self.code_exist_p):
            map(self.copy_code , self.List_code)
        else :
            print "No code files !"
            exit(0)
    
    def copy_code(self,item): #run in update
        directory = get_file_type_without_point(item)[1:]
        os.system ("cp -v  " + self.Desk_path + item + "  /Users/darkh/Dropbox/seed/"+ directory  + "/")
       # print  ("cp -v  " + self.Desk_path + item + "  /Users/darkh/Dropbox/seed/"+ directory  + "/")
        os.system ("rm   " + self.Desk_path + item )
        #print ("rm " + self.Desk_path +item)
    def format(self): #run in loop_look
        print_color =  color("blue")
        if (self.code_exist_p):
            for code_file in self.List_code : 
                print print_color.get_color(code_file),
        print

"""
this is for color
"""
class color:
    def __init__(self,color_i):
        self.color = color_i
    def get_color(self,st):
        if self.color == "blue":
            return  "%s[35;36m%s%s[0m" %(chr(27),st,chr(27))
        elif self.color == "green":
            return "%s[35;35m%s%s[0m" %(chr(27),st,chr(27))
        elif self.color == "black":
            return  "%s[5;30m%s%s[0m" %(chr(27),st,chr(27))

if __name__ == "__main__" :
    print_color_green = color("blue")
    os.system("clear")
    print print_color_green.get_color("Hi Sir, Qing luan zhi lv \n")
    process = file_code()
    print_color = color("green")
    if process.code_exist_p :
        Yes_or_No = 0
        while (Yes_or_No != "yes" or Yes_or_No != "no" or Yes_or_No != "Yes" or Yes_or_No != "No"):
            try:
                Yes_or_No = raw_input("Update code to Dropbox/seed ?"+ print_color.get_color("(Yes or no)  >>"))
            except KeyboardInterrupt :
                print print_color.get_color( "Goodbye Sir, Qingluan \n \t\t GoodNight!")
            if Yes_or_No == "yes" or Yes_or_No == "Yes":
                process.update()
                print print_color.get_color("\t\tGood Night !\n\t\t Qing Luan !\n")
                exit(0)
            else :
                print print_color.get_color("Goodbye Sir, Qingluan \n \t\t GoodNight!")
                exit(0)
                
